package com.centersoft.flowable.core.comm;


import lombok.Data;
import lombok.experimental.Accessors;

import javax.validation.constraints.NotNull;

/**
 * 标准的数据返回类
 *
 * @param <T> 标准类
 */
@Data
@Accessors(chain = true)
public class JsonResult<T> {
    /***
     * 结果编号
     * 详情请查看 StateCode
     * @see StateCode
     */
    private int code;
    /**
     * 消息
     */
    private String msg;
    /**
     * 数据
     */
    private T data;


    public static <T> JsonResult<T> success( String message,T data) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(StateCode.SUCCESS.getCode());
        result.setData(data);
        result.setMsg(message);
        return result;
    }
    public static <T> JsonResult<T> failure( String message,T data) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(StateCode.FAILURE.getCode());
        result.setData(data);
        result.setMsg(message);
        return result;
    }
    public static <T> JsonResult<T> success(String message) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(StateCode.SUCCESS.getCode());
        result.setMsg(message);
        return result;
    }

    public static <T> JsonResult<T> failure(String message) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(StateCode.FAILURE.getCode());
        result.setMsg(message);
        return result;
    }

    public static <T> JsonResult<T> failure(@NotNull StateCode code, String message) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(code.getCode());
        result.setMsg(message);
        return result;
    }

    public static <T> JsonResult<T> failure(@NotNull int code, String message) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(code);
        result.setMsg(message);
        return result;
    }
}
